﻿
Imports osi.root.connector
Imports osi.root.formation
Imports osi.root.delegates

Public Module _auto_disposer
    Public Function make_auto_disposer(Of T)(ByVal p As _do(Of T),
                                             Optional ByVal init As void = Nothing,
                                             Optional ByVal disposer As void(Of T) = Nothing) _
                                            As auto_disposer(Of T)
        Return New auto_disposer(Of T)(p, init, disposer)
    End Function

    Public Function make_auto_disposer(Of T)(ByVal p As T,
                                             Optional ByVal init As void = Nothing,
                                             Optional ByVal disposer As void(Of T) = Nothing) _
                                            As auto_disposer(Of T)
        Return New auto_disposer(Of T)(p, init, disposer)
    End Function
End Module

Public Class auto_disposer(Of T)
    Inherits disposer(Of T)

    Public Sub New(ByVal p As _do(Of T),
                   Optional ByVal init As void = Nothing,
                   Optional ByVal disposer As void(Of T) = Nothing)
        MyBase.New(p, init, disposer)
    End Sub

    Public Sub New(ByVal p As T,
                   Optional ByVal init As void = Nothing,
                   Optional ByVal disposer As void(Of T) = Nothing)
        MyBase.New(p, init, disposer)
    End Sub

    Protected NotOverridable Overrides Sub Finalize()
        'pointer will help to GC.KeepAlive
        dispose()
        MyBase.Finalize()
    End Sub
End Class
